reading-notes

How to Statically Generate Pages with Dynamic Routes

In our case, we want to create dynamic routes for blog posts:

We want each post to have the path /posts/, where is the name of the markdown file under the top-level posts directory. Since we have ssg-ssr.md and pre-rendering.md, we’d like the paths to be /posts/ssg-ssr and /posts/pre-rendering.

Overview of the Steps

We can do this by taking the following steps. You don’t have to make these changes yet — we’ll do it all on the next page.

In pages/posts/[id].js, we’ll write code that will render a post page — just like other pages we’ve created.

routes

Implement getStaticPaths

Create a file called [id].js inside the pages/posts directory. Also, remove first-post.js inside the pages/posts directory — we’ll no longer use this. Then, open pages/posts/[id].js in your editor and paste the following code. We’ll fill in … later